package com.demo.cityselect;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.demo.cityselect.cityselect.callback.OnCitySelectListener;
import com.demo.cityselect.cityselect.model.CityModel;
import com.demo.cityselect.cityselect.view.CitySelectView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements CitySeachListAdapter.OnClickItemlistener, OnCitySelectListener {

    @BindView(R.id.ed_city)
    EditText edCity;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.lin_top)
    LinearLayout linTop;
    @BindView(R.id.recy_city)
    CitySelectView recyCity;
    @BindView(R.id.lin_city)
    LinearLayout linCity;
    @BindView(R.id.seach_recy)
    RecyclerView seachRecy;
    private List<CityBean> mCityBeans;
    private CitySeachListAdapter mCitySeachListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        mCityBeans = new Gson().fromJson(ReturnUtils.getCityData(this), new TypeToken<List<CityBean>>() {
        }.getType());
        final List<CityModel> allCitys = new ArrayList<>();
        allCitys.clear();
        for (int i = 0; i < mCityBeans.size(); i++) {
            for (int j = 0; j < mCityBeans.get(i).getChildren().size(); j++) {
                allCitys.add(new CityModel(mCityBeans.get(i).getChildren().get(j).getText(), mCityBeans.get(i).getChildren().get(j).getValue()));

            }
        }
        final List<CityModel> hotCitys = new ArrayList<>();
        hotCitys.add(new CityModel("北京市", "110000"));
        hotCitys.add(new CityModel("上海市", "310000"));
        hotCitys.add(new CityModel("南京市", "320100"));
        hotCitys.add(new CityModel("杭州市", "330100"));
        hotCitys.add(new CityModel("广州市", "440100"));
        hotCitys.add(new CityModel("深圳市", "440300"));
        hotCitys.add(new CityModel("重庆市", "500100"));
        hotCitys.add(new CityModel("成都市", "510100"));

        recyCity.bindData(allCitys, hotCitys, null);
        recyCity.setOnCitySelectListener(this);
        mCitySeachListAdapter = new CitySeachListAdapter(this);
        seachRecy.setLayoutManager(new LinearLayoutManager(this));
        seachRecy.setAdapter(mCitySeachListAdapter);
        mCitySeachListAdapter.setOnClickItemlistener(this);
        initEdit();
    }

    private void initEdit() {
        edCity.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String city = editable.toString().trim();
                if (!TextUtils.isEmpty(city)) {
                    mCitySeachListAdapter.clear();
                    initseach(city);
                } else {
                    seachRecy.setVisibility(View.GONE);
                }

            }
        });
    }

    /**
     * 搜索城市
     */
    private void initseach(String city) {
        List<CitySeachBean> list = new ArrayList<>();
        list.clear();
        for (int i = 0; i < mCityBeans.size(); i++) {
            if (mCityBeans.get(i).getChildren() != null) {
                for (int j = 0; j < mCityBeans.get(i).getChildren().size(); j++) {
                    if (mCityBeans.get(i).getChildren().get(j).getText().contains(city)) {
                        list.add(new CitySeachBean(mCityBeans.get(i).getText(), mCityBeans.get(i).getValue(), mCityBeans.get(i).getChildren().get(j).getText(), mCityBeans.get(i).getChildren().get(j).getValue()));
                    }
                    if (mCityBeans.get(i).getChildren().get(j).getChildren() != null) {
                        for (int k = 0; k < mCityBeans.get(i).getChildren().get(j).getChildren().size(); k++) {
                            if (mCityBeans.get(i).getChildren().get(j).getChildren().get(k).getText().contains(city)) {
                                list.add(new CitySeachBean(mCityBeans.get(i).getText(), mCityBeans.get(i).getValue(), mCityBeans.get(i).getChildren().get(j).getChildren().get(k).getText(), mCityBeans.get(i).getChildren().get(j).getChildren().get(k).getValue()));
                            }
                        }
                    } else {
                        if (mCityBeans.get(i).getChildren().get(j).getText().contains(city)) {
                            list.add(new CitySeachBean(mCityBeans.get(i).getChildren().get(j).getText(), mCityBeans.get(i).getChildren().get(j).getValue(), "", 0));

                        }
                    }
                }
            }
        }
        if (list.size() != 0) {
            seachRecy.setVisibility(View.VISIBLE);
            mCitySeachListAdapter.setKeyword(city);
            mCitySeachListAdapter.addAll(list);
        } else {
            seachRecy.setVisibility(View.GONE);
        }
    }

    @Override
    public void onItem(String city, int code) {

    }

    @Override
    public void onCitySelect(CityModel cityModel) {

    }

    @Override
    public void onSelectCancel() {

    }
}
